package com.example.broadcasttest;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;
import android.content.BroadcastReceiver;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {
	
	private IntentFilter intentFilter;
	private NetworkChangeReceiver networkChangeReceiver;
	
	public void onCreate(Bundle savedInstance){
		super.onCreate(savedInstance);
		setContentView(R.layout.main_activity);
		
		intentFilter = new IntentFilter();
		intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
		networkChangeReceiver = new NetworkChangeReceiver();
		registerReceiver(networkChangeReceiver, intentFilter);
		
		Button button = (Button)findViewById(R.id.button);
		button.setOnClickListener(new OnClickListener(){
			public void onClick(View v){
				Intent intent = new Intent("com.example.broadcasttest.MY_BROADCAST");
			//	sendBroadcast(intent);
				sendOrderBroadcast(intent, null);
			}
		});
	}
	
	protected void onDestroy(){
		super.onDestroy();
		unregisterReceiver(networkChangeReceiver);
	}
	
	class NetworkChangeReceiver extends BroadcastReceiver{
		public void onReceive(Context context, Intent intent){
			ConnectivityManager connectionManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo networkInfo = connectionManager.getActiveNetworkInfo();
			if(networkInfo != null  && networkInfo.isAvailable()){
				Toast.makeText(context, "network is available", Toast.LENGTH_SHORT).show();
			} else {
				Toast.makeText(context, "network is unavailiable", Toast.LENGTH_SHORT).show();
			}
		}
	}
}
